<#include "/common/layout.html">
<@body>
<body class="gray-bg">
	<div class="wrapper">
		<section class="content">
			<div class="row">
				<div class="col-xs-12">
					<div class="box">
						<div class="form-inline">
							<div class="box-header">
								<div class="input-group">
									<#if shiro.hasPermission("addJob")>
										<a class="btn btn-sm btn-primary dialog" href="javascript:void(0);" data-url="${request.contextPath}/system/sysJob/add" data-title="创建任务"><i class="fa fa-plus"></i> 创建任务</a>
									</#if>
								</div>
								<div class="input-group input-group-sm">
									<input type="text" name="search_value" id="search_value" class="form-control" placeholder="任务名称">
									<div class="input-group-btn">
										<a href="javascript:;" id="search_data" class="btn btn-default" ><i class="fa fa-search"></i> 搜索</a>
										<a href="javascript:;" id="search_refresh" class="btn btn-default"><i class="fa fa-refresh"></i> 重置</a>
									</div>
								</div>
								<div class="input-group">
									<#if shiro.hasPermission("listJob")>
										<a class="btn btn-sm btn-default toNewTab" href="javascript:;" tab-url="${request.contextPath}/system/sysJobLog/list"><i class="fa fa-share"></i> 查看日志</a>
									</#if>
								</div>
							</div>
						</div>
						<div class="box-body">
							<table id="job_initTable" class="table table-bordered table-hover"></table>
						</div>
					</div>
				</div>
			</div>
		</section>
	</div>
</body>
</@body>
<script type="text/javascript">
	var editFlag = '${shiro.hasPermission("editJob")}';
	var deleteFlag = '${shiro.hasPermission("deleteJob")}';
	var query_data = {
		search: ''
	}
	var options = {
		id: "job_initTable",
		ajax: {
			url: "${request.contextPath}/system/sysJob/listPage",
			type: "POST",
			data: query_data,
		},
		headerCallback: function(e, a, t, n, s) {
			e.getElementsByTagName("th")[0].innerHTML = '<label class="m-checkbox"><input type="checkbox" class="minimal m-group-checkable" value="root"></label>';
			$(e.getElementsByTagName("th")).parent().css("background-color", "#f7f7fa");
		},
		columnDefs: [{
			targets: 0,
			width: "30px",
			orderable: !1,
			render: function(e, a, t, n) {
				return '<label class="m-checkbox"><input type ="checkbox" class="minimal m-checkable" value="' + t.id + '"></label>';
			}
		},{
			targets: 1,
			title: "行号",
			orderable: !1,
			render: function(e, a, t, n) {
				return n.row+1;
			}
		},{
			targets: 2,
			title: "任务名称",
			orderable: !1,
			render: function(e, a, t, n) {
				if (t.jobName != null) {
					return t.jobName;
				} else {
					return "";
				}
			}
		},{
			targets: 3,
			title: "任务分组",
			orderable: !1,
			render: function(e, a, t, n) {
				if (t.jobGroup != null) {
					return t.jobGroup;
				} else {
					return "";
				}
			}
		},{
			targets: 4,
			title: "执行表达式",
			orderable: !1,
			render: function(e, a, t, n) {
				if (t.cronExpression != null) {
					return t.cronExpression;
				} else {
					return "";
				}
			}
		},{
			targets: 5,
			title: "任务状态",
			orderable: !1,
			render: function(e, a, t, n) {
				if (t.status == 1) {
					return '<i class="fa fa-toggle-off text-info fa-2x" onclick="start(\''+ t.jobId + '\', \''+ t.jobGroup +'\')"></i>';
				} else {
					return '<i class="fa fa-toggle-on text-info fa-2x" onclick="stop(\''+ t.jobId + '\', \'' + t.jobGroup +'\')"></i>';
				}
			}
		},{
			targets: 6,
			title: "创建时间",
			orderable: !1,
			render: function(e, a, t, n) {
				if (t.createTime != null) {
					return t.createTime;
				} else {
					return '';
				}
			}
		},{
			targets: 7,
			title: "操作",
			orderable: !1,
			render: function(e, a, t, n) {
				var actions = [];
				if(editFlag == 'true') {
					actions.push('<a class="btn btn-info btn-xs" onclick="runTask(\''+ t.jobId + '\',\'' + t.jobGroup+ '\')"><i class="fa fa-play"></i>执行</a>')
				}
				if(editFlag == 'true') {
					actions.push( ' <a class="btn btn-primary btn-xs dialog" data-url="${request.contextPath}/system/sysJob/edit/' + t.jobId + '" data-title="编辑任务"><i class="fa fa-pencil"></i>编辑</a>')
				}
				if(deleteFlag == 'true') {
					actions.push( ' <a class="btn btn-danger btn-xs" delete-url="${request.contextPath}/system/sysJob/delete" data-id="'+ t.jobId +'"><i class="fa fa-trash"></i>删除</a>')
				}
				return actions.join('');
			}
		}]
	}
	//渲染表格
	data_oTable = $.table.init(options);
	//搜索
	$("#search_data").off().on("click", function() {
		query_data.search = $("#search_value").val();
		reloadTable();
	});
	//重置
	$("#search_refresh").off().on("click", function() {
		query_data.search = "";
		$("#search_value").val("");
		reloadTable();
	});
	//重新加载表格
	function reloadTable() {
		var oSetting = data_oTable.settings()[0];
		oSetting.ajax.data = query_data;
		data_oTable.draw();
	}
	// 选择图片
	function runTask(jobId, jobGroup){
		var url='${request.contextPath}/system/sysJob/run';
    	$.post(url, {"jobId": jobId, "jobGroup": jobGroup},function(json){
        	if(json.code==200){
        		$.modal.msgSuccess('执行成功');
        	}else{
        		$.modal.msgWarning(json.msg);
        	}
    	});
	}
	/* 调度任务-停用 */
	function stop(jobId, jobGroup) {
		$.modal.confirm('确认要停用该定时任务吗？', function(index){
	    	var url = '${request.contextPath}/system/sysJob/changeStatus';
        	$.post(url,{"jobId": jobId, "jobGroup": jobGroup, "status": 1 },function(json){
	        	if(json.code==200){
	        		$.modal.msgSuccess('停用成功');
	        		data_oTable.draw(false);
	        	}else{
	        		$.modal.msgWarning(json.msg);
	        	}
        	});
      	});
	}
	/* 调度任务-启用 */
	function start(jobId, jobGroup) {
		$.modal.confirm('确认要启用该定时任务吗？', function(index){
	    	var url = '${request.contextPath}/system/sysJob/changeStatus';
        	$.post(url,{"jobId": jobId, "jobGroup": jobGroup, "status": 0 },function(json){
	        	if(json.code==200){
	        		$.modal.msgSuccess('启用成功');
	        		data_oTable.draw(false);
	        	}else{
	        		$.modal.msgWarning(json.msg);
	        	}
        	});
		});
	}
</script> 